from appium import webdriver

import time

from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

desired_caps = dict()

# 设置中文可输入
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
# 配置 appium
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '192.168.247.101:5555'
# 设置页面
# desired_caps['appPackage'] = 'com.android.settings'
# desired_caps['appActivity'] = '.Settings'

# SD maid页面
# desired_caps['appPackage'] = 'eu.thedarken.sdm'
# desired_caps['appActivity'] = '.main.ui.SDMMainActivity'

# 设置滑动解锁图案页面
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.ChooseLockPattern'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 全局等待5s
driver.implicitly_wait(5)

# 1.1 轻敲操作 .tap(ele) 里面可以传元素属性，也可以传x，y的坐标值，count表示点击的次数
# 1》创建touchaction对象
# 2》通过对象调用想执行的手势
# 3》通过perform()执行，所有手势都要通过执行perform()函数才会运行
# ele = driver.find_element(By.XPATH, "//*[@text='语言和输入法']")
# TouchAction(driver).tap(ele).perform()
# TouchAction(driver).tap(x=300, y=1000, count=3).perform()
# time.sleep(3)
# TouchAction(driver).tap(x=300, y=1000, count=3).perform()


# 1.2 按下和抬起操作
# 获取屏幕大小
# window_size = driver.get_window_size()
# print(window_size)
# print('开始按压')
# 使用坐标形式按下WLAN(145，131)，2秒后，按下WLAN(145，131)，并抬起
# TouchAction(driver).press(x=145,y=131).perform()
# time.sleep(2)
# TouchAction(driver).press(x=145,y=131).release().perform()
# print('结束按压')

# 1.3 等待操作
# 使用坐标形式按下WLAN(145，131)，按退出
# 2秒后，按下WLAN(145，131)，等待2s，并抬起
# TouchAction(driver).tap(x=145, y=131).perform()
# time.sleep(1)
# TouchAction(driver).tap(x=20, y=45).perform()
# time.sleep(2)
# TouchAction(driver).press(x=145, y=131).wait(2000).release().perform()


# 1.4 长按操作
# 对设置里面的显示按钮进行长按,持续2s
# TouchAction(driver).long_press(x=136, y=227).release().perform()

# 1.5 移动操作
# 用按下-移动-抬起做手势解锁
# TouchAction(driver).press(x=214, y=427).move_to(x=295, y=427).move_to(x=381, y=427).move_to(x=295, y=511).move_to(x=214,
#                                                                               y=594).move_to(
#     x=296, y=594).move_to(x=385, y=594).release().perform()

TouchAction(driver).press(x=210, y=291).move_to(x=300, y=293).move_to(x=390, y=293).move_to(x=389, y=383).release().perform()
time.sleep(3)
driver.quit()
